﻿using System;
using System.Linq;

namespace Boxi.Model
{
    public struct PointModel
    {
        public Byte PointID
        {
            get
            {
                return _pointID;
            }
        }
        private Byte _pointID;
        /// <summary>
        /// 
        /// </summary>
        public Byte this[Byte i]
        {
            get
            {
                return _moves[i];
            }
        }
        private Byte[] _moves;

        /// <summary>
        /// Index of Top Move
        /// </summary>
        public Byte TopMoveID
        {
            get
            {
                return _moves[0];
            }
            set
            {
                _moves[0] = value;
            }
        }

        /// <summary>
        /// Index of Right Move
        /// </summary>
        public Byte RightMoveID
        {
            get
            {
                return _moves[1];
            }
            set
            {
                _moves[1] = value;
            }
        }

        /// <summary>
        /// Index of Bottom Move
        /// </summary>
        public Byte BottomMoveID
        {
            get
            {
                return _moves[2];
            }
            set
            {
                _moves[2] = value;
            }
        }

        /// <summary>
        /// Index of Left Move
        /// </summary>
        public Byte LeftMoveID
        {
            get
            {
                return _moves[3];
            }
            set
            {
                _moves[3] = value;
            }
        }
        public Boolean Contains(Byte moveID)
        {
            return _moves.Contains(moveID);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="columns">Count of Columns on Board</param>
        /// <param name="point">Count of Point on Board</param>
        /// <returns></returns>
        public PointModel(Byte pointID, Byte numberOfColumns)
        {
            _pointID = pointID;
            _moves = new Byte[4];
            Generate(numberOfColumns, pointID);
        }
        private void Generate(Byte numberOfColumns, Byte pointID)
        {
            if (pointID == 0)
                throw new Exception("point must be greater than 1");

            Int32 x = pointID % numberOfColumns;
            if (x == 0)
                x = numberOfColumns;

            Int32 y = pointID / numberOfColumns + ((x == numberOfColumns) ? 0 : 1);


            Int32 compleateColumn = numberOfColumns * 2 + 1;

            Int32 bottom = (y * compleateColumn) + x;

            _moves[0] = (Byte)(bottom - compleateColumn);
            _moves[1] = (Byte)(bottom - numberOfColumns);
            _moves[2] = (Byte)bottom;
            _moves[3] = (Byte)(bottom - numberOfColumns - 1);
        }
        public static Byte GetPointID(Int32 numberOfColumns, Byte column, Byte row)
        {
            return (Byte)((row - 1) * numberOfColumns + column);
        }
    }
}
